const http = require("http");
const webSocket = require("ws");

const server = http.createServer();
const wss = new webSocket.Server({ server });

wss.on("connection", (socket) => {
  console.log("1.websocket on");

  socket.on("message", (mes) => {
    console.log("receive message: ", mes);
    socket.send("hello, new friend");
  });

  socket.on("close", () => {
    console.log("connection closed");
  });
});

server.on("request", (req, res) => {
  res.writeHead(200, {
    "Content-type": "text/plain",
  });
  res.end("server: hellow,friend");
});

server.listen(80, () => {
  console.log("server on localhost:80");
});
